package com.jeesite.modules.codingrule.web;

import java.util.List;

import com.jeesite.common.utils.OrgUtil;
import com.jeesite.modules.codingrule.entity.qo.CodingRuleQO;
import com.jeesite.modules.codingrule.entity.vo.CodingRuleVO;
import com.jeesite.modules.codingrule.enums.BarcodeTypeEnum;
import com.jeesite.modules.codingrule.enums.CodingTypeEnum;
import com.jeesite.modules.codingrule.enums.ObjectTypeEnum;
import com.jeesite.modules.form.service.FormService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.codingrule.entity.CodingRuleHeader;
import com.jeesite.modules.codingrule.entity.CodingRuleLines;
import com.jeesite.modules.codingrule.service.CodingRuleHeaderService;

/**
 * 编码规则Controller
 * @author 码脑洞
 * @version 2025-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/codingrule/header")
public class CodingRuleHeaderController extends BaseController {

	@Autowired
	private CodingRuleHeaderService codingRuleHeaderService;
	@Autowired
	private FormService formService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public CodingRuleHeader get(Long autoId, boolean isNewRecord) {
		return codingRuleHeaderService.get(autoId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("codingrule:header:view")
	@RequestMapping(value = {"list", ""})
	public String list(CodingRuleHeader codingRuleHeader, Model model) {
		model.addAttribute("codingRuleHeader", codingRuleHeader);
		model.addAttribute("formList", formService.selectFormList());
		model.addAttribute("objectTypeEnum", ObjectTypeEnum.toCommonSelectDataVOList());
		model.addAttribute("barcodeTypeEnum", BarcodeTypeEnum.toCommonSelectDataVOList());
		return "modules/codingrule/codingRuleHeaderList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("codingrule:header:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<CodingRuleVO> listData(CodingRuleQO codingRuleQO, HttpServletRequest request, HttpServletResponse response) {
		codingRuleQO.setPage(new Page<>(request, response));
		Page<CodingRuleVO> page = codingRuleHeaderService.findCodingRulePage(codingRuleQO);
		return page;
	}
	
	/**
	 * 查询子表数据
	 */
	@RequiresPermissions("codingrule:header:view")
	@RequestMapping(value = "linesListData")
	@ResponseBody
	public Page<CodingRuleLines> subListData(CodingRuleLines codingRuleLines, HttpServletRequest request, HttpServletResponse response) {
		codingRuleLines.setPage(new Page<>(request, response));
		Page<CodingRuleLines> page = codingRuleHeaderService.findSubPage(codingRuleLines);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("codingrule:header:view")
	@RequestMapping(value = "form")
	public String form(CodingRuleHeader codingRuleHeader, Model model) {
		if (OrgUtil.isValid()){
			codingRuleHeader.setOrgId(OrgUtil.getOrgId());
			codingRuleHeader.setOrgCode(OrgUtil.getOrgCode());
			codingRuleHeader.setOrgName(OrgUtil.getOrgName());
		}
		model.addAttribute("codingRuleHeader", codingRuleHeader);
		model.addAttribute("formList", formService.selectFormList());
		model.addAttribute("objectTypeEnum", ObjectTypeEnum.toCommonSelectDataVOList());
		model.addAttribute("barcodeTypeEnum", BarcodeTypeEnum.toCommonSelectDataVOList());
		model.addAttribute("codingTypeEnum", CodingTypeEnum.toCommonSelectDataVOList());
		return "modules/codingrule/codingRuleHeaderForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("codingrule:header:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated CodingRuleHeader codingRuleHeader) {
		codingRuleHeaderService.save(codingRuleHeader);
		return renderResult(Global.TRUE, text("保存编码规则成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("codingrule:header:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(CodingRuleHeader codingRuleHeader) {
		codingRuleHeaderService.delete(codingRuleHeader);
		return renderResult(Global.TRUE, text("删除编码规则成功！"));
	}
	
}